package net.hn.hnms.biz.plan.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import net.hn.hnms.biz.plan.domain.YieldPeelTemporary;
import net.hn.hnms.biz.plan.mapper.YieldPeelTemporaryMapper;
import net.hn.hnms.biz.plan.request.FootageYieldReq;
import net.hn.hnms.biz.plan.request.YieldPeelTemporaryReq;
import net.hn.hnms.biz.plan.service.YieldPeelTemporaryService;
import net.hn.hnms.biz.plan.vo.FootageYieldCountVo;
import net.hn.hnms.biz.plan.vo.YieldPeelTemporaryLineChartVo;
import net.hn.hnms.biz.plan.vo.YieldPeelTemporaryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@DS("master")
public class YieldPeelTemporaryServiceImpl implements YieldPeelTemporaryService {

    @Autowired
    private YieldPeelTemporaryMapper yieldPeelTemporaryMapper;

    @Override
    public List<YieldPeelTemporary> selectList(YieldPeelTemporaryReq req) {


        return yieldPeelTemporaryMapper.selectList(req);
    }

    @Override
    public YieldPeelTemporaryVo yieldCount(YieldPeelTemporaryReq req) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date;

        if (StrUtil.isBlank(req.getYear()) || StrUtil.isBlank(req.getMonth())) {
            date = new Date();
        } else {
            try {
                date = sdf.parse(req.getYear() + "-" + (Integer.parseInt(req.getMonth()) < 10 ? "0" + req.getMonth() : req.getMonth()) + "-01");
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        //月初
        DateUtil.format(DateUtil.beginOfMonth(date), "yyyy-MM-dd");
        //月末
        DateUtil.format(DateUtil.endOfMonth(date), "yyyy-MM-dd");

        req.setYearBeginDate(DateUtil.format(DateUtil.beginOfYear(date), "yyyy-MM-dd"));
        req.setYearEndDate(DateUtil.format(DateUtil.endOfYear(date), "yyyy-MM-dd"));

        req.setMonthBeginDate(DateUtil.format(DateUtil.beginOfMonth(date), "yyyy-MM-dd"));
        req.setMonthEndDate(DateUtil.format(DateUtil.endOfMonth(date), "yyyy-MM-dd"));

        YieldPeelTemporaryVo yieldPeelTemporaryVo = yieldPeelTemporaryMapper.yieldCount(req);
        yieldPeelTemporaryVo.setYieldOverabundance(yieldPeelTemporaryVo.getDayYieldCount().subtract(yieldPeelTemporaryVo.getDayYieldPlan()));
        yieldPeelTemporaryVo.setPeelOverabundance(yieldPeelTemporaryVo.getDayPeelCount().subtract(yieldPeelTemporaryVo.getDayPeelPlan()));
        return yieldPeelTemporaryVo;
    }

    @Override
    public List<YieldPeelTemporaryLineChartVo> yieldOrPeelLineChart(YieldPeelTemporaryReq req) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date;

        if (StrUtil.isBlank(req.getYear())) {
            date = new Date();
        } else {
            try {
                date = sdf.parse(req.getYear() + "-01-01");
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

        req.setYearBeginDate(DateUtil.format(DateUtil.beginOfYear(date), "yyyy-MM-dd"));
        req.setYearEndDate(DateUtil.format(DateUtil.endOfYear(date), "yyyy-MM-dd"));


        return yieldPeelTemporaryMapper.yieldOrPeelLineChart(req);
    }

    @Override
    public FootageYieldCountVo yestDayCount(FootageYieldReq req) {


        return yieldPeelTemporaryMapper.yestDayCount(req);
    }

}
